package com.game.sdk.pay.heepay;

import java.io.InputStream;


import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.game.sdk.YTAppService;
import com.game.sdk.domain.ParamJson;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.init.NetCallBack;
import com.game.sdk.util.Constants;
import com.game.sdk.util.DataSafeUtil;
import com.game.sdk.util.GetDataImpl;
import com.game.sdk.util.NetTask;
import com.game.sdk.util.Util;

public class NetUtil {
	private static final String TAG = "yeepayNetUtil";
	private static NetUtil getdataImpl;
	private static Context ctx;

	private NetUtil(Context ctxs) {
		this.ctx = ctxs;
	}	
	
	public static NetUtil getInstance(Context ctxs) {
		if (null == getdataImpl) {
			getdataImpl = new NetUtil(ctxs);
		}
		if (ctx==null) {
			ctx=ctxs;
		}
		return getdataImpl;
	}
	/**
	 * 请求汇付宝 
	 * 
	 * @param mem_id 
	 * @param user_token
	 * @param amount
	 * @param pay_token
	 * @param ctx
	 * @param callback
	 * @return
	 */
	public static void reqHeepay(String mem_id, String user_token,
			String amount, String pay_token, final Context ctx,String paytype,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getHeepayParamsObjs(mem_id, user_token, amount, pay_token,paytype)
				.toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_CHARGER_HEEPAY, jasonStr, true, true,
				true, true);
	}

	/**
	 * 汇付宝参数的获取
	 * 
	 * @param mem_id
	 * @param pay_token
	 * @param amount
	 * @param pay_token
	 * */
	private static Object getHeepayParamsObjs(String mem_id, String user_token,
			String amount, String pay_token,String paytype) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setAmount(amount);
		
		json.setUserid(mem_id);

		json.setUser_token(user_token);

		json.setCode(DataSafeUtil.code + "");

		json.setFrom(ParamJson.ANDROID+"");
		
		json.setClient_id(YTAppService.clientId + "");
		
		String api_token = DataSafeUtil.getApiToken("heepay",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);
		if("30".equals(paytype)){
			json.setPayway("heepay");
		}
		if("22".equals(paytype)){
			json.setPayway("heepayali");
		}
		json.setPay_token(pay_token);
		
		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}
	/**
	 * 请求汇付宝快捷支付
	 * 
	 * @param mem_id 
	 * @param user_token
	 * @param amount
	 * @param pay_token
	 * @param ctx
	 * @param callback
	 * @return
	 */
	public static void reqHeepayQuick(String mem_id, String user_token,
			String amount, String pay_token, String paytype,final Context ctx,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getHeeQuickParamsObjs(mem_id, user_token, amount, pay_token,paytype)
				.toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_CHARGER_HEEPAY, jasonStr, true, true,
				true, true);
	}

	/**
	 * 汇付宝参数的获取
	 * 
	 * @param mem_id
	 * @param pay_token
	 * @param amount
	 * @param pay_token
	 * */
	private static Object getHeeQuickParamsObjs(String mem_id, String user_token,
			String amount, String pay_token,String paytype) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setAmount(amount);
		
		json.setUserid(mem_id);

		json.setUser_token(user_token);

		json.setCode(DataSafeUtil.code + "");

		json.setFrom(ParamJson.ANDROID+"");
		
		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("heepay",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);
		
		json.setPayway(paytype);
		
		json.setPay_token(pay_token);
		
		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}
	/**
	 * 请求汇付宝收银台
	 * 
	 * @param mem_id 
	 * @param user_token
	 * @param amount
	 * @param pay_token
	 * @param ctx
	 * @param callback
	 * @return
	 */
	public static void reqHeepayAll(String mem_id, String user_token,
			String amount, String pay_token, String paytype,final Context ctx,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getHeeAllParamsObjs(mem_id, user_token, amount, pay_token,paytype)
				.toString();
		new NetTask() {
			
			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_CHARGER_HEEPAY, jasonStr, true, true,
				true, true);
	}
	
	/**
	 * 汇付宝参数的获取
	 * 
	 * @param mem_id
	 * @param pay_token
	 * @param amount
	 * @param pay_token
	 * */
	private static Object getHeeAllParamsObjs(String mem_id, String user_token,
			String amount, String pay_token,String paytype) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);
		
		json.setAmount(amount);
		
		json.setUserid(mem_id);
		
		json.setUser_token(user_token);
		
		json.setCode(DataSafeUtil.code + "");
		
		json.setFrom(ParamJson.ANDROID+"");
		
		json.setClient_id(YTAppService.clientId + "");
		
		String api_token = DataSafeUtil.getApiToken("heepay",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);
		
		json.setPayway(paytype);
		
		json.setPay_token(pay_token);
		
		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));
		
		return json.buildParams();
		
	}
}
